//
//  CatagoryController.m
//  Kugou-OC
//
//  Created by BH on 16/5/27.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "CatagoryController.h"
#import "GlobalDefine.h"
#import "CustomImageButton.h"
#import "NetworkHelper.h"
#import "CatagoryModel.h"
#import "CatagoryCell.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>
#import <SDAutoLayout/UITableView+SDAutoTableViewCellHeight.h>

@interface CatagoryHeaderView : UIView{
    CustomImageButton *classicbtn;//经典
    UIButton *netBtn;//网络
    UIButton *danceBtn;//舞曲
    UIButton *sentimentBtn;//伤感
    UIButton *nintyBtn;//90后
    UIButton *quietBtn;//安静
    UIButton *englishBtn;//英语
}


@end

@implementation CatagoryHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        classicbtn = (CustomImageButton *)[self setBtnWithTitle:@"经典" color:COLOR(235, 164, 112, 1) tag:1];
        netBtn = [self setBtnWithTitle:@"网络" color:COLOR(136, 205, 143, 1) tag:2];
        danceBtn = [self setBtnWithTitle:@"舞曲" color:COLOR(244, 195, 95, 1) tag:3];
        sentimentBtn = [self setBtnWithTitle:@"伤感" color:COLOR(105, 182, 194, 1) tag:4];
        nintyBtn = [self setBtnWithTitle:@"90后" color:COLOR(135, 202, 189, 1) tag:5];
        quietBtn = [self setBtnWithTitle:@"安静" color:COLOR(226, 119, 146, 1) tag:6];
        englishBtn = [self setBtnWithTitle:@"英语" color:COLOR(84, 128, 208, 1) tag:7];
        
        [self sd_addSubviews:@[classicbtn,netBtn,danceBtn,sentimentBtn,nintyBtn,quietBtn,englishBtn]];
        
        classicbtn.sd_layout.leftSpaceToView(self,15).topSpaceToView(self,15).widthIs((SCREEN_WIDTH-35)/2).heightEqualToWidth();
        netBtn.sd_layout.leftSpaceToView(classicbtn,5).topEqualToView(classicbtn).widthIs(((SCREEN_WIDTH-35)/2-5)/2).heightIs(((SCREEN_WIDTH-35)/2-10)/3);
        danceBtn.sd_layout.leftSpaceToView(netBtn,5).topEqualToView(netBtn).bottomEqualToView(netBtn).widthIs(netBtn.width);
        sentimentBtn.sd_layout.leftEqualToView(netBtn).topSpaceToView(netBtn,5).rightEqualToView(netBtn).heightIs(netBtn.height);
        nintyBtn.sd_layout.leftEqualToView(danceBtn).topEqualToView(sentimentBtn).rightEqualToView(danceBtn).bottomEqualToView(sentimentBtn);
        quietBtn.sd_layout.leftEqualToView(sentimentBtn).topSpaceToView(sentimentBtn,5).rightEqualToView(sentimentBtn).bottomEqualToView(classicbtn);
        englishBtn.sd_layout.leftEqualToView(nintyBtn).topEqualToView(quietBtn).rightEqualToView(nintyBtn).bottomEqualToView(quietBtn);
    }
    
    return self;
}

-(UIButton *)setBtnWithTitle:(NSString *)title color:(UIColor *)color tag:(NSInteger)tag{
    UIButton *button = ALLOC(UIButton);
    button.tag = tag;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = color;
    button.titleLabel.font = TITLE_FOUR_FONT;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

-(void)buttonClick:(UIButton *)button{
    
}

@end

/*--------------------------------------------------------------------------------------------------------------------------------------------------*/
#define kTimeLineTableViewCellId @"CatagoryCell"

@interface CatagoryController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *catagoryTable;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation CatagoryController
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

-(UITableView *)catagoryTable{
    if (_catagoryTable == nil) {
        _catagoryTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-90-64-30) style:UITableViewStylePlain];
        _catagoryTable.dataSource = self;
        _catagoryTable.delegate = self;
        
        CatagoryHeaderView *headerView = [[CatagoryHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_WIDTH-35)/2+30)];
        _catagoryTable.tableHeaderView = headerView;
        
    }
    
    return _catagoryTable;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.catagoryTable];
    
    [self getData];
}

-(void)getData{
    NSString *urlStr = @"http://ioscdn.kugou.com/api/v3/tag/list";
    NSDictionary *param = @{@"plat":@"2",@"apiver":@"2"};
    
    [[NetworkHelper shareNetwork] getDataWithUrl:urlStr params:param view:self.view success:^(id response) {
        
        NSArray *info = response[@"data"][@"info"];
        for (NSDictionary *dict in info) {
            CatagoryModel *model = [[CatagoryModel alloc] initWithDict:dict];
            [self.dataArray addObject:model];
        }
        
        [self.catagoryTable reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

#pragma mark - UITableView datasource & delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CatagoryCell *cell = [CatagoryCell cellWithTableView:tableView];
    
    cell.children = [self.dataArray[indexPath.section] childrens];
    cell.indexPath = indexPath;
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    sectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *sectionLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, sectionView.width-15, 35)];
    sectionLab.font = TITLE_THIRD_FONT;
    sectionLab.textColor = BG_COLOR;
    sectionLab.text = [self.dataArray[section] catagoryName];
    [sectionView addSubview:sectionLab];
    
    return sectionView;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.dataArray[section] catagoryName];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger count = [self.dataArray[indexPath.section] childrens].count;
    if (indexPath.section == 0) {
        return (count/2+count%2)*30+10;
    }else{
        return (count/4+(count%4?1:0))*30+10;
    }
}

/**
 *  重载scrollview的delegate方法，取消section header悬停
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

- (CGFloat)cellContentViewWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 适配ios7
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}

@end
